package org.SimpleSpringBoot.container;

import org.SimpleSpringBoot.annotation.SimpleSpringBootApplication;
import org.SimpleSpringBoot.config.AutoConfiguration;

import java.util.List;

/*
应用上下文，springboot的核心容器
 */
public class ApplicationContext {
    private final BeanFactory beanFactory;

    private final ComponentScanner componentScanner;

    private final Class<?> primarySource;


    public ApplicationContext(Class<?> primarySource) {
        this.primarySource = primarySource;
        this.beanFactory=new BeanFactory();
        this.componentScanner=new ComponentScanner();

        //初始化应用上下文
        initialize();
    }


    /*
    初始化应用上下文
     */
    private void initialize(){
        System.out.println("开始初始化应用上下文");
        //1.确定扫描的基础包
        String basePackage= determineBasePackage();
        System.out.println("扫描基础包:"+basePackage);

        //2扫描组件
        List<Class<?>> componennts=componentScanner.scanComponents(basePackage);
        System.out.println("扫描到："+componennts.size()+"个组件");

        //3注册Bean定义
        registerBeanDefinitions(componennts);


        //4 执行自动装配
        executeAutoConfiguration();

        //5 初始化所有单例Bean
        initializeSingleonBeans();

        System.out.println("应用上下文初始化完成");


    }

    /*
    确定基础扫描包
     */
    private String determineBasePackage() {
        SimpleSpringBootApplication annotation = primarySource.getAnnotation(SimpleSpringBootApplication.class);

        if (annotation!=null&&annotation.scanBasePackages().length>0){
            return annotation.scanBasePackages()[0];
        }
        //默认使用主类所在的包
        return primarySource.getPackage().getName();
    }
    /**
     * 注册Bean定义
     */
    private void registerBeanDefinitions(List<Class<?>> components){
        for (Class<?> component:components){
            String beanName = componentScanner.getBeanName(component);
            BeanDefinition beanDefinition = new BeanDefinition(beanName, component);
            beanFactory.registerBeanDefinnition(beanName,beanDefinition);
        }
    }
    /**
     * 执行自动装配
     */
    private void executeAutoConfiguration(){
        System.out.println("执行自动装配。。。");
        AutoConfiguration autoConfiguration = new AutoConfiguration();
        autoConfiguration.configure(this);
    }
    /**
     * 初始化所有单利Bean
     */

    private void initializeSingleonBeans(){
        System.out.println("初始化单利Bean");
        for (String beanName : beanFactory.getAllBeanDefinitions().keySet()) {

            try {
                beanFactory.getBean(beanName);
            }catch (Exception e){
                System.err.println("初始化Bean失败："+beanName+"错误"+e.getMessage());
            }
        }
    }
    /**
     * 获取Bean实例
     */
    public Object getBean(String beanName){
        return beanFactory.getBean(beanName);
    }

    /**
     * 根据类型获取Bean
     */
    public <T> T getBean(Class<T> requiredType){
        return beanFactory.getBean(requiredType);
    }

    /**
     * 检查是否包含指定的Bean
     */
    public boolean containsBean(String beanName){
        return beanFactory.containsBean(beanName);
    }

    /**
     * 获取Bean工厂
     */
    public BeanFactory getBeanFactory(){
        return beanFactory;
    }









}
